package com.da.lon.wang.xlg.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.da.lon.wang.xlg.MyApplication;
import com.da.lon.wang.xlg.R;
import com.da.lon.wang.xlg.activity.ProductsActivity;
import com.da.lon.wang.xlg.activity.WebViewActivity;
import com.da.lon.wang.xlg.constants.PreferencesConfig;
import com.da.lon.wang.xlg.constants.XLGConstants;
import com.da.lon.wang.xlg.util.PreferenceUtils;
import com.da.lon.wang.xlg.util.Utils;
import com.umeng.analytics.MobclickAgent;

import java.util.Locale;


public class PartyFragment extends BaseFragment {

    private WebView webView;
    private View view;
    private LinearLayout party_content;
    private ImageView frush_img;

    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("PartyFragment");
        MobclickAgent.onResume(getActivity());
        // initView();
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("PartyFragment");
        MobclickAgent.onPause(getActivity());
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        initView(view);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected View inflateView(LayoutInflater inflater, ViewGroup var2) {
        view = inflater.inflate(R.layout.ocoffee_party_fragment, null);
        return view;
    }

    @Override
    protected void initView() {
        initView(view);
    }

    private void initView(View view) {
        try {
            party_content = (LinearLayout) view
                    .findViewById(R.id.party_content);
            frush_img = (ImageView) view.findViewById(R.id.frush_img);
            frush_img.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    load();
                }
            });
            webView = (WebView) view.findViewById(R.id.ocoffee_party_wv);

            webView.getSettings().setUseWideViewPort(true);
            webView.getSettings().setLoadWithOverviewMode(true);
            webView.getSettings().setBuiltInZoomControls(false);

            webView.getSettings().setJavaScriptEnabled(true);// 启用javascript支持
            webView.setWebChromeClient(new WebChromeClient() {
                @Override
                public void onProgressChanged(WebView view, int newProgress) {
                    if (newProgress == 100) {
                        dismissDialog();
                    } else
                        showDialog();
                }
            });
            webView.setWebViewClient(new WebViewClient() {
                @Override
                public boolean shouldOverrideUrlLoading(WebView view,
                                                        String path) {
                    if (path != null && !path.isEmpty()
                            && path.toUpperCase(Locale.CHINA).contains(// 商品跳转
                            "/p/".toUpperCase(Locale.CHINA))) {
                        if (null == getActivity()) {
                            return true;
                        }
                        Intent intent = new Intent(getActivity(),
                                ProductsActivity.class);
                        String product_number;
                        if (path.indexOf(".html") != -1) {
                            product_number = path.substring(
                                    path.indexOf("/p/") + 3,
                                    path.indexOf(".html"));
                        } else {
                            product_number = path.substring(
                                    path.indexOf("/p/") + 3, path.length());
                        }
                        PreferenceUtils.getInstance().SetSettingString(
                                PreferencesConfig.PRODUCT_NUMBER,
                                product_number);
                        ((Activity) getActivity()).startActivity(intent);
                        return true;

                    } else {
                        Intent intent = new Intent(getActivity(),
                                WebViewActivity.class);
                        Bundle bundle = new Bundle();
                        bundle.putInt("flag", 002);
                        bundle.putString("url", path);
                        intent.putExtras(bundle);
                        startActivity(intent);
                        return true;
                    }
                }
            });
            load();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void load() {
        if (!MyApplication.isNetworkAvailable(getActivity())) {
            showDialog();
            party_content.setVisibility(View.GONE);
            frush_img.setVisibility(View.VISIBLE);
            new Thread() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(2000);
                        dismissDialog();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }.start();
        } else {
            party_content.setVisibility(View.VISIBLE);
            frush_img.setVisibility(View.GONE);
            webView.loadUrl(Utils.constructUrl(XLGConstants.WEB_URL
                    + "Activity-index.html?"));
        }
    }

    @Override
    protected void RefreshData() {
        // TODO Auto-generated method stub

    }

}
